/**
 * Created by Administrator on 2017/8/1.
 */
define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var $ = require('jquery');
	var util = require('util');
	var popwin	= require('@base/dialog');
	//溢价规则弹窗
	var premiumType = {
		'channel':1,
		'spot':2,
		'deal':3
	};
	var premiumNum = {
		1:'channel',
		2:'spot',
		3:'deal'
	};
	var Base = popwin.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'title': LANG('溢价规则'),
				'size': 'l',
				'vModel':{
					'materials': [],
					'_UserRadio': 0,
				},
				'vMethods':{
				},
				updateUrl:'premium/update',
				addUrl:'premium/add',
				infoUrl:'premium/info',
				hasFocusFirstInput: false
			});
			this.$id = null;
			this.$isEdit = null;
			this.$tabType = 'channel';
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			self.Super('afterBuild', arguments);
			var el = self.getDOM();
			var c = self.getConfig();

			$([
				'<div class="M-formCon">',
					'<div class="M-formConGroup P-adsAddChannelCon">',
						'<label class="M-formLabel group-label">'+LANG('名称：')+'</label>',
						'<div class="group-content">',
							'<span pub-mod="@base/common/input.text" pub-name="Name"/>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">'+LANG('子账号：')+'</label>',
						'<div class="group-content">',
							'<div class="M-formSectionItem">',
								'<label>',
									'<input type="radio" v-model="_UserRadio" value="0">',
									'<span class="ml5">'+LANG('所有子账号')+'</span>',
									'</label>',
								'<label>',
									'<input type="radio" v-model="_UserRadio" value="1">',
									'<span class="ml5">'+LANG('部分子账号')+'</span>',
								'</label>',
							'</div>',
							'<div v-show="_UserRadio!=0" class="mt20">',
								'<div class="">',
									'<span pub-name="Select" pub-mod="modules/selectMore.base"></span>',
								'</div>',
							'</div>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup P-adsAddChannelCon">',
						'<label class="M-formLabel group-label">'+LANG('溢价维度选择：')+'</label>',
						'<div class="group-content">',
						'<div class="M-formTab" pub-name="tab" pub-mod="@base/common/base.tab"/>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup P-adsAddChannelCon">',
						'<label class="M-formLabel group-label">'+LANG('溢价比例：')+'</label>',
						'<div class="group-content">',
							// '<span>'+ LANG('溢价') +'</span>',
							'<span pub-mod="@base/common/input.text" pub-name="Value"/>',
						'</div>',
					'</div>',
				'</div>',
			].join('')).appendTo(el);
			self.buildFromTemplate({
				Name: {
					width: '50%',
					value: LANG('新建溢价规则_')+ new Date().getTime()
				},
				Select:{
					'cols':[
						{name:'_id',text:"ID", sort: true, render: 'center',align: "center", width: 60},
						{name:'UserName',text:LANG("名称"), type:'index', render: this.renderName, align: "left"},
					] // 可以覆盖
					,'url':'user/list'  //列表路径
				},
				Value:{
					'type':'text',
					'beforeText': '',
					'afterText': '%',
					'width': '60',
					'value': 0
				},
				tab: {
					'class': 'cool',
					'reqMethod': 'websocket',
					'silent': false,
					'list': {
						'channel':{
							'text': LANG('渠道'),
							'module': 'admin/premium/tab/tab.channel'
						},
						'spot':{
							'text': LANG('广告位'),
							'module': 'admin/premium/tab/tab.spot'
						},
						'deal':{
							'text': LANG('订单'),
							'module': 'admin/premium/tab/tab.deal'
						},
					}
				},
			});
			self.vm.mount();
		},
		setData: function(data){
			data = util.clone(data);
			var self = this;
			var vm = self.vm;
			var cs = self.$;
			var type = self.toType(data.Type);
			cs.Select.setData(data.userObject);
			cs.Name.setValue(data.Name);
			if(data.userObject && data.userObject.length){
				vm.$._UserRadio = 1;
			}
			cs.tab.switchTab(type);
			cs.tab.setData(data.Ids, type);
			cs.Value.setValue(data.Value);
			return this;
		},
		getData: function () {
			var data = {};
			var vm = this.vm.$;
			var cs = this.$;
			var name =cs.Name.getValue();
			data.Name = util.trim(cs.Name.getValue());
			data.UserIds = cs.Select.getData(true);
			// 渠道/广告位/订单 三选一
			var tabData = this.$.tab.getData();
			data.Ids = [];
			util.each(tabData[this.$tabType], function(item){
				data.Ids.push(+item);
			});
			// 溢价类型
			data.Type = this.toNum(this.$tabType);
			data.Value = +util.trim(cs.Value.getValue());
			return data;
		},
		validate: function(data){
			data = data || this.getData();
			if(!data.Name){
				pubjs.alert("请填写名称");
				return false;
			}
			if(data.UserIds && !data.UserIds.length){
				pubjs.alert("子账号将默认为全部");
			}
			if(!data.Value){
				pubjs.alert("请设置溢价比例");
				return false;
			}
			if(data.Value< -100 ||data.Number>1000){
				pubjs.alert("溢价比例超出范围");
				return false;
			}
			if(data.Ids && !data.Ids.length){
				pubjs.alert("请选择溢价维度");
				return false;
			}
			return true;
		},
		save: function(){
			var self = this;
			var cs = self.$;
			var vm = self.vm.$;
			var data = self.getData();
			var c = self.getConfig();
			if(this.validate(data)){
				self.showLoading();
				if(self.$isEdit){
					data._id = self.$id;
					pubjs.mc.send(c.updateUrl, data, self.onSave.bind(self));
				}else{
					pubjs.mc.send(c.addUrl, data, self.onSave.bind(self));
				}
			}
		},
		onOk:function () {
			this.save()
		},
		onSave: function(err, data){
			this.hideLoading();
			if (err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.core.cast("premiumListReload");
			this.hide();
		},
		cancel: function(){
		},
		reset: function(){
			var self = this;
			var cs = self.$;
			self.vm.reset();
			cs.Name.setValue(LANG('新建溢价规则_')+ new Date().getTime());
			cs.tab.reset();
			cs.Select.reset();
			cs.Value.reset();
			this.$id = null;
			this.$isEdit = null;
			this.$tabType = 'channel';
			cs.tab.switchTab('channel');
			return self;
		},
		load: function(id, isCopy) {
			var self = this;
			var c = self.getConfig();
			self.$isEdit = isCopy ? false:true;
			self.$id = +id;
			self.showLoading();
			pubjs.mc.send(c.infoUrl, {_id: +id}, self.onData.bind(self), isCopy);
			return self;
		},
		onData: function(err, data, isCopy) {
			this.hideLoading();
			if(err) {
				return pubjs.alert(err.message);
			}
			// 复制需要删除ID
			if (isCopy){
				data._id = null;
			}
			this.setData(data);
		},
		//tab切换的时候,缓存当前tab
		onTabChange:function (ev) {
			if(ev && ev.param && ev.param.name){
				this.$tabType = ev.param.name || 'channel';
			}
		},
		//转化type
		toType:function (num) {
			return premiumNum[num];
		},
		toNum:function (type) {
			return premiumType[type];
		}
	});
	exports.base = Base;
});